/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.applet;

/**
 *
 * @author Mr.Sam
 */
import java.applet.*;
import java.awt.*;
import fr.ove.applet.ALCAppletStub;

/**
* A "wrapping" applet displaying information about the status of
* the loading of the real applet to load.<BR>
* This applet takes as parameters :
* <UL>
* <LI> <CODE>doCheck11</CODE> to force checking of support of JDK 1.1. Must be setted as <CODE>true</CODE>
* or <CODE>false</CODE> </LI>.
* <LI> <CODE>theAppletName</CODE> which is the name of the applet to load </LI>.
* </UL>
*
* @author © 1999 DIRAT Laurent
* @version 1.0  30/04/99
*/
public class AppletLoadingChecked extends Applet {
    /**
    * The different states during the loading of the applet.
    */
    public final static int LOADING                = 0;
    public final static int LOADED                 = 1;
    public final static int ERROR                  = 2;
    public final static int JDK11_NOT_SUPPORTED    = 3;

    /**
    * The message to display during the loading.
    */
    private String message;
    private int messageWidth;
    private FontMetrics fm = null;

    /**
    * The image to display during the loading (typically a timer)
    */
    private Image image;
    private int imageWidth;
    private int imageHeight;

    /**
    * The applet we really want to load
    */
    private Applet theApplet;

    /**
    * The name of the applet (More exactly the name of the class)
    */
    private String theAppletName;

    /**
    * The state of the applet.
    */
    protected int state = LOADING;

    public void init() {
        setBackground(Color.white);
        setLayout(new BorderLayout(0, 0));

        updateMessage();

        image = getImage(getCodeBase(), "sablier.gif");

        MediaTracker tracker = new MediaTracker(this);
        tracker.addImage(image,0);
        try {
            tracker.waitForAll();
        }
        catch (InterruptedException e) {
        }

        if (image != null) {
            imageWidth = image.getWidth(this);
            imageHeight = image.getHeight(this);
        }

        String doCheck11 = getParameter("doCheck11");
        if ((doCheck11 != null) && (doCheck11.equals("true"))) {
            try {
                Class class1 = Class.forName("java.awt.AWTEvent");
                class1 = Class.forName("java.awt.event.ActionEvent");
            }
            catch (Exception e) {
                state = JDK11_NOT_SUPPORTED;
                updateMessage();
                return;
            }
        }

        Thread t = new Thread() {
            public void run() {
                while (true) {
                    if (state != LOADING) {
                        this.stop();
                    }
                    repaint();
                    try {
                        Thread.sleep(500);
                    }
                    catch (InterruptedException e) {
                    }
                }
            }
        };
        t.start();

        Thread t2 = new Thread() {
            public void run() {
                try {
                    theAppletName = getParameter("theAppletName");
                    theApplet = (Applet) Class.forName(theAppletName).newInstance();
                    theApplet.setStub(new ALCAppletStub(AppletLoadingChecked.this));
                    Dimension size = getSize();
                    theApplet.reshape(0, 0, size.width - 1, size.height - 1);
                    theApplet.hide();
                    add("Center", theApplet);
                    theApplet.init();
                    theApplet.show();
                    validate();
                    state = LOADED;
                } catch (Exception e) {
                    state = ERROR;
                    updateMessage();
                    System.err.println(e);
                }
            }
        };
        t2.start();
    }

    public void start() {
        if (state == LOADED)
          theApplet.start();
    }

    public void stop() {
        if (state == LOADED)
          theApplet.stop();
    }

    public void paint(Graphics g) {
        Dimension size = getSize();

        if (state == LOADED) {
            theApplet.paint(g);
        }
        else if (state == LOADING) {
            if (image != null)
                g.drawImage(image, size.width/2 - imageWidth/2, size.height/2 - imageHeight, this);
            g.drawString(message, size.width/2 - messageWidth/2, size.height/2 + 20);
        }
        else {
            g.setColor(Color.white);
            g.fillRect(0, 0, size.width - 1, size.height - 1);
            g.setColor(Color.red);
            g.drawString(message, size.width/2 - messageWidth/2, size.height/2);
        }
    }

    public void update(Graphics g) {
        paint(g);
    }

    private void updateMessage() {
        if (fm == null)
            fm = getFontMetrics(getFont());

        switch (state) {
            case LOADING :
                message = "Please wait while loading...";
                break;
            case ERROR :
                message = "Something wrong occurs :o(";
                break;
            case JDK11_NOT_SUPPORTED :
                message = "Your browser do not support Java 1.1";
                break;
        }

        messageWidth = fm.stringWidth(message);
    }

    /**
    * Returns the state of the applet.
    */
    public int getState() {
        return state;
    }

    /**
    * Sets the state of the applet.
    */
    public void setState(int state) {
        this.state = state;
        updateMessage();
        repaint();
    }

    /**
    * Returns the "wrapped" applet
    */
    public Applet getTheApplet() {
        return theApplet;
    }
}
